//
//  ByUserMyVC.m
//  jinpuyiyuan_ios
//
//  Created by mc on 2017/12/4.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByUserMyVC.h"
// view
#import "ByViewMyHeader.h"

// vc
#import "ByUserMySetVC.h"               // 设置
#import "ByUserMyRealNameFirstVC.h"     // 就诊卡登记
#import "ByUserMyRecordDetailVC.h"
#import "ByUserMyFeedbackVC.h"          // 意见反馈
#import "ByLoginVC.h"

#import "ByNetConfig.h"

@interface ByUserMyVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) ByViewMyHeader *headerView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, copy)   NSArray *titles;
@property (nonatomic, copy)   NSArray *images;

@end

@implementation ByUserMyVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    [self addBYItemviews:@[@"My_shezhi"] mode:ByNavigationItemModeImage position:ByNavigationItemPositionModeRight];
    [self createViews];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNaviAttributeWithTranslucent:YES stateBarLightContent:YES];
    ByModelUser *user = [ByCacheUser getUserInfo];
    if (user){
        self.headerView.model = user;
    }
}

- (void)rightItemsAction:(NSInteger)rightIndex{
    if ([self isLogin]) {
        ByUserMySetVC *vc = [[ByUserMySetVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
   
}
#pragma mark - getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@[@"就诊卡登记"],
                    @[@"处方记录",@"化验记录",@"住院记录"],
                    @[@"意见反馈",@"联系我们"]];
    }
    return _titles;
}

- (NSArray *)images{
    if (!_images) {
        _images = @[@[@"My_jiuzhen"],
                    @[@"My_chufang",@"My_huayan",@"My_zhuyuan"],
                    @[@"My_ideas",@"My_contact"]];
    }
    return _images;
}

#pragma mark - create views
- (void)createViews{
    self.headerView = [[ByViewMyHeader alloc] init];
    [self.view addSubview:self.headerView];
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(0));
        make.left.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 300));
    }];
    
    self.tableView = [[UITableView alloc] init];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.top.equalTo(self.headerView.mas_bottom);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-49);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = BY_TABLE_COLOR;
    self.tableView.separatorColor = [UIColor colorWithWhite:0.90 alpha:1];
    self.tableView.separatorInset = UIEdgeInsetsMake(49, 10, 1, 10);
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titles.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *rowCount = self.titles[section];
    return rowCount.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *indentier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:indentier];
        cell.textLabel.font = FONT(14);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.imageView.image = [UIImage imageNamed:self.images[indexPath.section][indexPath.row]];
    cell.textLabel.text = self.titles[indexPath.section][indexPath.row];
    if (indexPath.section == 0) {
        cell.detailTextLabel.text = @"实名制认证";
        cell.detailTextLabel.font = FONT(14);
    }else{
        cell.detailTextLabel.text = @"";
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        cell.detailTextLabel.text = [ByNetConfig getCustomerPhone];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!(indexPath.section == 2 && indexPath.row == 1)) {
        if (![self isLogin]) {
            return;
        }
    }
    // 就诊卡登记
    if (indexPath.section == 0) {
        ByUserMyRealNameFirstVC *vc = [[ByUserMyRealNameFirstVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    else if (indexPath.section == 1){
        // 处方记录
        if (indexPath.row == 0) {
            ByUserMyRecordDetailVC *vc = [[ByUserMyRecordDetailVC alloc] init];
            vc.title = @"处方记录";
            [self by_normalPush:vc hasNavi:YES];
        }
        // 化验记录
        else if (indexPath.row == 1){
            
        }
        // 住院记录
        else{
            ByUserMyRecordDetailVC *vc = [[ByUserMyRecordDetailVC alloc] init];
            vc.title = @"住院记录";
            [self by_normalPush:vc hasNavi:YES];
        }
    }
    else{
        // 意见反馈
        if (indexPath.row == 0) {
            ByUserMyFeedbackVC *vc = [[ByUserMyFeedbackVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
        }
        // 联系我们
        else{
            
        }
    }
}

#pragma mark - 判断是否登录
- (BOOL)isLogin{
    ByModelUser *user = [ByCacheUser getUserInfo];
    if (user) {
        return YES;
    }else{
        ByLoginVC *vc = [[ByLoginVC alloc] init];
        vc.typeFrom = @"1";
        ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
        [self presentViewController:navi animated:YES completion:nil];
        return NO;
    }
    
}
@end
